<?php

class MAITutors extends FTAObject {
		
		var $renderTutorsFunction;
		var $tutor;
	
		function MAITutors(){
			parent::__construct();
				
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
			
			global $FTADateFormat;
			$this->dateFormat = (0 < strlen($FTADateFormat)) ? $FTADateFormat : "d F y";
		}
		
		function getTutorByUrlFragment($urlfragment){
			if (is_null($urlfragment))
				return false;
				
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docTutor');
			
			$this->cmsDocument->addConstraint('title',$urlfragment);
			
			$this->cmsDocument->setPageSize(1);
			
			$this->cmsDocument->resetIterator();
			
			if($this->cmsDocument->totalDocuments() == 1) {
				$this->tutor = $this->cmsDocument->nextDocumentFromCache();
				return true;
			} else {
				$this->tutor = array();		
				return false;
			}
		}
		
		function getTutors($pageSize = null, $pageNumber = null, $sort = null, $direction = null){
			//this gets sponsors and filters based on the category if there is one set.. 
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docTutor');
						
			if(!is_null($sort)){
				if(!is_null($direction)){
					$this->cmsDocument->addSort($sort, $direction);
				} else {
					$this->cmsDocument->addSort($sort);
				}
			} else {
				$this->cmsDocument->addSort("content");
			}
			
			$this->cmsDocument->setPageSize($pageSize);
			$this->cmsDocument->setPageNumber($pageNumber);
			
			$this->cmsDocument->resetIterator();
			
			$allDocuments = $this->cmsDocument->getAllDocuments();
			
			if (!$allDocuments)
				$allDocuments = array();
				
			return $allDocuments;
						
		}
		
		
		function renderTutorsList(){
			$tutors = $this->getTutors();
			
			if(is_null($this->renderTutorsFunction)) {
				
				global $MAITutorsItemRenderFunction;
				if(!isset($MAITutorsItemRenderFunction)){
					$MAITutorsItemRenderFunction = '$o=fta_getSharedTutorsObject(); echo $o->renderTutorsItemFunction($item);';
				}
				//echo "Into Here!";
				$this->renderTutorsFunction  = create_function('$item', $MAITutorsItemRenderFunction );
			}
				
			//we need to map the links and do some funky scope wrandling..
			mai_tutors_widgetList($tutors,"tutor-list", null, null, null,$this->renderTutorsFunction);
		}
		
		
		
}

	function mai_renderTutorsList(){
		ob_start();
		$tutorObject = mai_getSharedTutorsObject();
		$tutorObject->renderTutorsList();
		return ob_get_clean();	
	}


	function mai_getSharedTutorsObject(){

		global $fta_;

		global $MAITutorsClass;
		
		if(!is_null($MAITutorsClass) && "MAISponsors" != $MAITutorsClass){
		} else {
			$MAITutorsClass = "MAITutors";
		}
			
		if (!array_key_exists('maiTutors',$fta_)){
			$fta_->maiTutors = new  $MAITutorsClass();
		}
		return $fta_->maiTutors;
	}

	function mai_tutors_widgetList($items,$listClass="list",$altItemClass='altItem',$firstItemClass='firstItem', $lastItemClass='lastItem',$renderItemFunction) {
?>
<ul id="<?= $listClass ?>">
<?		
	$row = -1;
	$numRows = count($items);
	
	foreach($items as $item){
		
		$itemClasses=array();
		
		if ($altItemClass && 0 == ($row++) % 2 ) 
			array_push($itemClasses,$altItemClass);
		
		if ($firstItemClass && 0 == $row)
			array_push($itemClasses,$firstItemClass);			
			
		if ($lastItemClass && $numRows -1 == $row)
			array_push($itemClasses,$lastItemClass);

?>		
		<li <?= ( 0 < count($itemClasses) ? "class=\"" . implode(" ",$itemClasses) . "\"" : "") ?> ><?= $renderItemFunction( $item ) ?></li>		
<?		
	}
?>
</ul>
<?		
	}
?>